package com.sgb.kjwl.view.ui.adapter.manage;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemWebsiteReleaseListBinding;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.model.entity.manage.GloryHonorAdminEntity;
import com.sgb.kjwl.model.entity.manage.IPDetailEntity;
import com.sgb.kjwl.model.entity.manage.PromotionalAlbumEntity;
import com.sgb.kjwl.model.entity.manage.UpdataSpecialQualificationEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.sgb.kjwl.viewmodel.manage.WebsiteReleaseModule;
import com.sgb.member.entity.OperatingResultsEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.DateUtils;

import java.util.List;

import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.HANGYERONGYU;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.QIYEDONGTAI;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.QIYEXIANGCE;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.QIYEXINXI;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.QIYEZIZHI;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.YEJIZHENGMING;
import static com.sgb.kjwl.view.ui.activity.manage.WebsiteReleaseActivity.ZHISHICHANQUAN;

public class WebsiteReleaseListAdapter<T> extends BaseQuickAdapter<T, BaseViewHolder> {

    private WebsiteReleaseModule websiteReleaseModule;
    private GridLayoutManager manager;
    private RequestManager requestManager;

    public WebsiteReleaseListAdapter(WebsiteReleaseModule websiteReleaseModule, List<T> data) {
        super(R.layout.item_website_release_list, data);
        this.websiteReleaseModule = websiteReleaseModule;
    }

    public void setManager(GridLayoutManager manager, RequestManager requestManager) {
        this.manager = manager;
        this.requestManager = requestManager;
    }

    @Override
    protected void convert(BaseViewHolder helper, T item) {
        ItemWebsiteReleaseListBinding binding = DataBindingUtil.bind(helper.itemView);
        switchDataView(binding, item, helper.getLayoutPosition());
        binding.setViewModel(websiteReleaseModule);
        binding.executePendingBindings();
    }

    private void switchDataView(ItemWebsiteReleaseListBinding binding, T item, int position) {
        switch (websiteReleaseModule.modeltype) {
            case QIYEXIANGCE: //企业相册
                binding.rlImage.setVisibility(View.VISIBLE);
                binding.lineView.setVisibility(View.GONE);
                setQiYeXiangCe(binding, item, position);
                break;
            case QIYEXINXI://信息简介
                setQiYeXinXi(binding, item);
                break;
            case QIYEZIZHI: //专项资质
                binding.rlZxzz.setVisibility(View.VISIBLE);
                setQiYeZiZhi(binding, item);
                break;
            case YEJIZHENGMING: //经营业绩
                binding.rlJxyj.setVisibility(View.VISIBLE);
                setYeJiZhengMing(binding, item);
                break;
            case QIYEDONGTAI: //经营动态
                binding.rlJydt.setVisibility(View.VISIBLE);
                setQiYeDongTai(binding, item);
                break;
            case ZHISHICHANQUAN: //知识产权
                binding.rlZscq.setVisibility(View.VISIBLE);
                setZhiShiChanQuan(binding, item);
                break;
            case HANGYERONGYU: //荣誉荣耀
                binding.rlRyry.setVisibility(View.VISIBLE);
                setHangYeRongYu(binding, item);
                break;
        }
    }

    /**
     * 企业相册
     *
     * @param binding
     * @param item
     */
    private void setQiYeXiangCe(ItemWebsiteReleaseListBinding binding, T item, int position) {
        ViewGroup.LayoutParams params = binding.getRoot().getLayoutParams();
        params.height = (int)((manager.getWidth() / manager.getSpanCount()
                - 2 * binding.getRoot().getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) params).leftMargin) * 0.68);
        ImageView ivPhoto = binding.ivPhoto;
        PromotionalAlbumEntity entity = (PromotionalAlbumEntity) item;
       int type = entity.getType();//1图片2视频3vr实景
        if (type == 1){
            binding.ivPlayer.setVisibility(View.GONE);
            setImageView(ivPhoto,entity.getUrl());
        } else if (type == 2){
            ivPhoto.setImageResource(R.mipmap.bg_jyb_error_iamge);
            binding.ivPlayer.setVisibility(View.VISIBLE);
            binding.ivPlayer.setImageResource(R.mipmap.bofang);
            setImageView(ivPhoto,entity.getVideoCoverUrl());
        } else {
            ivPhoto.setImageResource(R.mipmap.bg_jyb_error_iamge);
            binding.ivPlayer.setVisibility(View.VISIBLE);
            binding.ivPlayer.setImageResource(R.drawable.icon_vr);
            setImageView(ivPhoto,entity.getUrl());
        }

        ivPhoto.setOnClickListener(v -> {
            if (!ClickUtils.isDoubleClick()) {
                if (type == 1) {
                    websiteReleaseModule.openPhotoView(mContext, position, (List<Object>) getData());
                } else if (type == 2) {
                    websiteReleaseModule.openVideoPlayView(mContext, entity);
                } else {
                    WebViewX5HostActivity.start(mContext, entity.getVrUrl());
                }
            }
        });
        setLongItemListener(ivPhoto, entity.getId() + "");


    }

    /**
     * 信息简介
     *
     * @param binding
     * @param item
     */
    private void setQiYeXinXi(ItemWebsiteReleaseListBinding binding, T item) {
    }

    /**
     * 专项资质
     */
    private void setQiYeZiZhi(ItemWebsiteReleaseListBinding binding, T item) {
        UpdataSpecialQualificationEntity entity = (UpdataSpecialQualificationEntity) item;
        binding.setSpecialQualificationEntity(entity);
        setImageView(binding.ivZxzzCompanyIcon,entity.getImage());
        String text = "";
        if (!TextUtils.isEmpty(entity.getType())) { // 类型
            text = entity.getType();
        }
        if (!TextUtils.isEmpty(entity.getRecordEnterName())) { // 单位
            text = TextUtils.isEmpty(text) ? entity.getRecordEnterName() : text + "   |   " + entity.getRecordEnterName();
        }
        if (!TextUtils.isEmpty(entity.getQualificationClassName())) { // 等级
            text = TextUtils.isEmpty(text) ? entity.getQualificationClassName() : text + "   |   " + entity.getQualificationClassName();
        }
        binding.tvZxzzNameCompany.setText(text);

        setItemListener(binding.getRoot(), binding.tvZxzzDelete, binding.tvZxzzUpdate, entity.getId() + "", "资质");
    }

    /**
     * 经营业绩
     */
    private void setYeJiZhengMing(ItemWebsiteReleaseListBinding binding, T item) {
        ImageView imStart = binding.imState;
        OperatingResultsEntity entity = (OperatingResultsEntity) item;
        binding.setEntityOperating(entity);

        switch (entity.getStatus()) {
            case 0:
                Glide.with(mContext).load(R.mipmap.operating_status_0).into(imStart);
                break;
            case 1:
                Glide.with(mContext).load(R.mipmap.operating_status_1).into(imStart);
                break;
            case 2:
                Glide.with(mContext).load(R.mipmap.operating_status_2).into(imStart);
                break;
            default:
                imStart.setVisibility(View.GONE);
                break;
        }
        setItemListener(binding.getRoot(), binding.tvJxyjDelete, binding.tvJxyjUpdate, entity.getId() + "", "经营");
    }

    /**
     * 经营动态
     *
     * @param binding
     * @param item
     */
    private void setQiYeDongTai(ItemWebsiteReleaseListBinding binding, T item) {
        CompanyTradesListEntity entity = (CompanyTradesListEntity) item;
        binding.setCompanyTradesListEntity(entity);
        setImageView(binding.ivItemImgCompanyIcon,entity.getCover());
        binding.tvItemNameCompany.setText(TextViewBindingAdapter.setForSpace(entity.getUserName() + "     " + DateUtils.timestampTo13Date3(entity.getCreateTime())));
        setItemListener(binding.getRoot(), binding.tvJydtDelete, binding.tvJydtUpdate, entity.getId() + "", "经营");
    }

    /**
     * 知识产权
     *
     * @param binding
     * @param item
     */
    private void setZhiShiChanQuan(ItemWebsiteReleaseListBinding binding, T item) {
        IPDetailEntity ipDetailEntity = (IPDetailEntity) item;
        binding.setIpDetailEntity(ipDetailEntity);
        setImageView(binding.ivZscqCompanyIcon, ipDetailEntity.getUploadPictures());
        binding.patentType.setText(ipDetailEntity.getType());
        switch (ipDetailEntity.getTypesOfPropertyRights()) {
            case "商标":
            case "版权":
                binding.llField.setVisibility(View.GONE);
                binding.llState.setVisibility(View.GONE);
                break;
            case "专利":
                binding.llField.setVisibility(View.VISIBLE);
                binding.llState.setVisibility(View.VISIBLE);
                binding.fieldCategory.setText(ipDetailEntity.getFieldClassName());
                if (ipDetailEntity.getState().isEmpty()) {
                    binding.patentState.setText("");
                } else if (ipDetailEntity.getState().equals("2")) {
                    binding.patentState.setText("授权已下证");
                } else if (ipDetailEntity.getState().equals("1")) {
                    binding.patentState.setText("授权未下证");
                }
                break;
        }
        setItemListener(binding.getRoot(), binding.tvZscqDelete, binding.tvZscqUpdate, ipDetailEntity.getId() + "", ipDetailEntity.getTypesOfPropertyRights());
    }

    /**
     * 荣誉荣耀
     *
     * @param binding
     * @param item
     */
    private void setHangYeRongYu(ItemWebsiteReleaseListBinding binding, T item) {
        GloryHonorAdminEntity industryHonoursEntity = (GloryHonorAdminEntity) item;
        binding.setGloryHonorAdminEntity(industryHonoursEntity);
        setImageView(binding.ivRyryCompanyIcon, industryHonoursEntity.getHonorPictures());
        binding.tvRyryCategory.setText(TextViewBindingAdapter.setTextForColon("资质类型:" + industryHonoursEntity.getHonorClassification()));
        binding.tvRyryCompanyAddress.setText(TextViewBindingAdapter.setTextForColon("颁发时间:" + DateUtils.timestampToDate(industryHonoursEntity.getDateOfIssue())));
        binding.tvLine.setVisibility(View.VISIBLE);
        if (industryHonoursEntity.getIndustryTypeNo() == null || industryHonoursEntity.getIndustryTypeNo().equals("")) {
            binding.tvLine.setVisibility(View.GONE);
            binding.tvRyryType.setVisibility(View.GONE);
        } else {
//            binding.tvTradeType.setText(entity.getStaffNumber() + "人");
            binding.tvRyryType.setVisibility(View.VISIBLE);
        }
        if (industryHonoursEntity.getHonorClassification() == null || industryHonoursEntity.getHonorClassification().equals("")) {
            binding.tvLine.setVisibility(View.GONE);
            binding.tvRyryCategory.setVisibility(View.GONE);
        } else {
//            binding.tvCategory.setText(tradeHonorEntity.getCategory());
            binding.tvRyryCategory.setVisibility(View.VISIBLE);
        }
        setItemListener(binding.getRoot(), binding.tvRyryDelete, binding.tvRyryUpdate, industryHonoursEntity.getId() + "", "荣誉");
    }

    private void setImageView(ImageView ivPhoto,String imageUrl){
        RequestOptions options = new RequestOptions();
        options.override(400, 400);
        options.transform(new RoundedCorners(5));
        options.error(mContext.getResources().getDrawable(R.mipmap.bg_jyb_error_iamge));
        options.placeholder(mContext.getResources().getDrawable(R.mipmap.bg_jyb_error_iamge));
        if (requestManager == null ){
            requestManager = Glide.with(mContext);
        }
        requestManager.load(imageUrl).apply(options).into(ivPhoto);
    }


    /**
     * 列表点击时间响应
     *
     * @param rome   条目点击布局
     * @param del    删除按钮
     * @param update 编辑按钮
     * @param id     当前操作ID
     * @param name   当前操作模块名称
     */
    private void setItemListener(View rome, View del, View update, String id, String name) {
        rome.setOnClickListener(v -> {
            if (onClickListener != null) {
                onClickListener.onRoomClick(id, name);
            }
        });
        del.setOnClickListener(v -> {
            if (onClickListener != null) {
                onClickListener.onDel(id);
            }
        });
        update.setOnClickListener(v -> {
            if (onClickListener != null) {
                onClickListener.onEditor(id, name);
            }
        });
    }

    /**
     * 列表长按响应
     *
     * @param del 删除按钮
     * @param id  当前操作ID
     */
    private void setLongItemListener(View del, String id) {
        del.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onDel(id);
                }
                return false;
            }
        });
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public OnClickListener onClickListener;

    public interface OnClickListener {
        void onDel(String id); // 删除

        void onEditor(String id, String name); // 编辑

        void onRoomClick(String id, String name); // 条目点击
    }

}
